package koalacam.server;

import java.net.URLEncoder;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 考勤记录
 * @author Wangshutao
 * 2016年7月5日
 */
public class Attendance {
	
	/**
	 * 考勤记录 按用户名模糊查询
	 * @param cookieStore cookie值
	 * @param url 
	 * @param username 用户名
	 * @throws Exception
	 */
	public static void records(CookieStore cookieStore,String url,String username) throws Exception{
		System.out.println("Start GET /attendance/records 考勤记录 ...");
		
		//自定义HttpClients 设置CookieStore 
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        //添加参数 username
        HttpGet request = new HttpGet(url + "?user_name="+URLEncoder.encode(username,"UTF-8"));
        
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析返回结果
        JSONObject resp = new JSONObject(responseBody);
        int result = resp.optInt("code", -1);
        if (result != 0) {
            System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
        }else{
        	JSONArray array = resp.getJSONArray("data");
        	for (int i = 0; i < array.length(); i++) {
				JSONObject info = (JSONObject)array.get(i);
				System.out.println("id:"+info.getJSONObject("subject").getInt("id") + ",date:" + info.getInt("date") + ",worktime:" + info.getString("worktime"));
			}	
        }
	}

}
